﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MallInteractiveMap.CMSWebClient.Common;
using MallInteractiveMap.CMSWebClient.Presenter;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;

namespace MallInteractiveMap.CMSWebClient.BasicView
{
    public partial class Category : BasePage
    {
        private CategoryPresenter _presenter; 
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new CategoryPresenter();
            if (!IsPostBack)
            {
                PrepareView();
            }
        }

        public int Id
        {
            get { return Request.QueryString["Id"] != null ? Convert.ToInt32(Request.QueryString["Id"]) : 0; }
        }

        private void PrepareView()
        {
            if (Id > 0)
            {
                DTOCategory cagegory = _presenter.GetCategorys(Id).FirstOrDefault();
                PopulateControls(cagegory);
            }

        }
        private void PopulateControls(DTOCategory category)
        {
            Name.Text = category.Name;
            Description.Text = category.Description;
        }
        private DTOCategory PopulateObject()
        {
            DTOCategory category = new DTOCategory();
            category.Name = Name.Text;
            category.Description = Description.Text;
            category.LastUpdated = DateTime.Now;
            category.IsNew = Id <= 0;
            category.Id = Id;
            return category;
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            DTOCategory category = PopulateObject();
            if (_presenter.SetData(category))
            {
                Response.Redirect("~/BasicView/CategoryDetail.aspx");
            }
            else
            {
                ShowMessage(LanguageLoader.GetText("FAILED_OPERATION_MESSAGE"), LanguageLoader.GetText("FAILED"));
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            SetLanguageAndResource();
        }

        private void SetLanguageAndResource()
        {
            NameLable.Text = LanguageLoader.GetText("NAME");
            DescriptionLable.Text = LanguageLoader.GetText("DESCRIPTION");
            SaveMall.Text = LanguageLoader.GetText("SAVE");
        }
    }
}